# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.pro>

_realName=RuC-VM
_realname=${_realName,,}
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.2
pkgrel=1
pkgdesc="Virtual machine for Russian version of C (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://github.com/andrey-terekhov/RuC-VM'
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("${_realName}-${pkgver}.tar.gz::${url}/archive/v${pkgver}.tar.gz"
        '0001-ruc-vm-1.2-gnu-install-dirs.patch'
	'0002-ruc-vm-1.2-libruc-vm-prefix.patch')
sha256sums=('9aa43dd12092b562e2165a373141871b6b48806a075a92a1b7cb0865c491a9e1'
	    'c91727acc4bf2b30ce5911150da35eda832b1bab33e1cd8a2b8bb292707e9f0d'
            'e2a362b8a83071a87e67b20de2484729b720314832228885a50576ac09d65f03')

prepare() {
  cd "${srcdir}"/${_realName}-${pkgver}

  patch -Np1 -i "${srcdir}"/0001-ruc-vm-1.2-gnu-install-dirs.patch
#  patch -Np1 -i "${srcdir}"/0002-ruc-2.4.3-win32.patch
  patch -Np1 -i "${srcdir}"/0002-ruc-vm-1.2-libruc-vm-prefix.patch
}

build() {
  cd "${srcdir}/${_realName}-${pkgver}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_{SHARED,STATIC}_LIBS=ON \
      ../${_realName}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
